<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button id="btn">按钮</button>
    <script>
      const btn = document.getElementById('btn')
      btn.onclick = function () {
        const xhr = new XMLHttpRequest()
        xhr.open('get', 'http://localhost:5000/a')

        //设置超时时间
        xhr.timeout = 2000 // 2s
        xhr.ontimeout = function () {
          // 请求超时触发
          alert('请求超时了')
        }

        xhr.send()
      }
    </script>
  </body>
</html>
